package com.example.validatestudy.demos.dataStructures;

import java.util.Stack;

/**
 * @param
 * @version v1
 * @auther WANGZHENFANG004
 * @summary
 * @since 2024 06 24 13 36
 */
public class StackTest {

    /**
     * 栈顶元素
     * E peek();  获取栈顶元素，不删除元素  peek /piːk/v 偷看，窥视；微露出，探出
     * 出栈
     * E pop();
     * 入栈
     * E push();
     * 空判断  empty()和isEmpty()一样
     * boolean isEmpty(); 判断是否为空，Vector实现
     * boolean empty(); 判断是否为空，Stack实现
     * 查找元素
     * int search(1);  for循环查找元素，返回坐标
     * @param args
     */
    public static void main(String[] args) {
        Stack<Integer> in  = new Stack<>();
        in.push(10);
        //获取栈顶元素，不删除元素  peek /piːk/v 偷看，窥视；微露出，探出
        in.peek();
        //出栈
        in.pop();
        //入栈 push有返回值，add返回boolen
        in.push(10);
        //in.add(10);
        //判断是否为空,empty()和isEmpty()一样，一个是Stack实现，一个是Vector实现
        in.empty();
        in.isEmpty();
        //for循环查找元素，返回坐标
        in.search(1);

        queueTest();
    }

    private static void queueTest(){
        MyQueue queue = new MyQueue();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.peek());
        queue.push(4);
        queue.push(5);
        queue.push(6);
        System.out.println(queue.peek());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println( queue.peek());
        //预测结果 1 1 2 3 -- 3 3 4 5
    }

    /**
     * 自定义队列
     * 用到Stack的push()\peek()\pop()
     */
    private static class MyQueue{
        private Stack<Integer> in  = new Stack<>();
        private Stack<Integer> out = new Stack<>();

        public Integer push(Integer a){
            in.push(a);
            return a;
        }

        public Integer peek(){
            in2Out();
           return out.peek();
        }

        public Integer pop(){
            in2Out();
            return out.pop();
        }

        private void in2Out(){
            if(out.isEmpty()){
                while (!in.isEmpty()){
                    out.push(in.pop());
                }
            }
        }

    }

}
